package cn.tedu.boot23.controller;

import cn.tedu.boot23.utils.DBUtils;
import cn.tedu.boot23.vo.Emp;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;

@RestController
public class EmpController {
    @RequestMapping("/insert")
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        try(Connection conn = DBUtils.getConn()) {
            String sql = "insert into myemp1 values(null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加完成!<a href='/'>回到首页</a>";
    }
    @RequestMapping("/select")
    public String select(){
        ArrayList<Emp> list = new ArrayList<>();
        try(Connection conn = DBUtils.getConn()) {
            String sql = "select * from myemp1";
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()){
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String job = rs.getString(3);
                int sal = rs.getInt(4);
                list.add(new Emp(id,name,job,sal));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String html = "<table border=1>";
        html+="<caption>员工列表</caption>";
        html+="<tr><th>id</th><th>姓名</th><th>工作</th><th>工资</th><th>删除</th></tr>";
        for (Emp emp:list) {
            html+="<tr>";
            html+="<td>"+emp.getId()+"</td>";
            html+="<td>"+emp.getName()+"</td>";
            html+="<td>"+emp.getJob()+"</td>";
            html+="<td>"+emp.getSal()+"</td>";
            html+="<td><a href='/delete?id="+emp.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        System.out.println("emp = " + emp);
        try(Connection conn = DBUtils.getConn()) {
            String sql = "update myemp1 set name=?,job=?,sal=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            ps.setInt(4,emp.getId());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "修改完成!<a href='/select'>返回列表</a>";
    }

    @RequestMapping("/delete")
    public String delete(int id){
        try(Connection conn = DBUtils.getConn()) {
            String sql = "delete from myemp1 where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除完成<a href='/select'>返回列表</a>";
    }
}
